/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package data;


import java.io.*;
import java.util.ArrayList;
import java.util.List;
import sqlcommand.*;

/**
 *
 * @author GREENWIND
 */
public class ManagerDatabase implements Serializable {

    private List<Table> tables;
    private int nTable;
    private ObjectOutputStream oos;
    private ObjectInputStream ois;
    private String name;
    private Command sqlCommand;

    /**
     * lay tat cac bang
     * @param file: ten CSDL
     * @return //true: thanh cong, false: that bai
     */
    public ManagerDatabase(String Name) {
        tables = new ArrayList<Table>();
        nTable = 0;
        this.name = Name;
    }

    public ManagerDatabase(List<Table> t) {
        tables = t;
        nTable = t.size();
    }

    public Boolean addNewTable(Table table) {
        
        if (getAllTables().add(table)) {
            nTable++;
            return true;
        } else {
            return false;
        }
    }

    public Boolean loadAllTable(String file) {
        try {
            ois = new ObjectInputStream(new FileInputStream(new File(file)));

            Object ob = ois.readObject();
            while (ob != null) {
                getAllTables().add((Table) ob);
                nTable++;
                ob = ois.readObject();
            }
        } catch (EOFException ex) {
            ois.close();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            System.out.println(ex.toString());
            ex.printStackTrace();
        } finally {
            //Close the ObjectInputStream
            try {
                if (ois != null) {
                    ois.close();
                }
                return true;
            } catch (IOException ex) {
                ex.printStackTrace();
                return false;
            }
        }
    }

    public Boolean saveAllTable(String file)  {
        try {
            oos = new ObjectOutputStream(new FileOutputStream(new File(file)));
            for (int i = 0; i < this.tables.size(); i++) {
                oos.writeObject(getAllTables().get(i));
            }
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
            return false;
        } catch (IOException ex) {
            ex.printStackTrace();
            return false;
        } finally {
            //Close the ObjectOutputStream
            try {
                if (oos != null) {
                    oos.flush();
                    oos.close();
                }
                return true;
            } catch (IOException ex) {
                ex.printStackTrace();
                return false;
            }
        }


    }

    /**
     * 
     * @param table 
     * @param cols cac cot chua dieu kien( 1 cot co the xuat hien nhieu lan
     * @param conditions cac dieu kien
     * @param operator cac toan tu <, <=, >, =>, !=, =
     * @param connective tu noi AND, OR
     * @return cac dong thoa dieu kien, (lay tat ca cac cot cua dong)
     */
    public List<Row> selectFromMutileTable(            
            String[] table,
            String[] cols, 
            String[] conditions, 
            String[] operator,  
            String[] connective) { 
        List<Row> result = new ArrayList<Row>();        
        switch (table.length) {
            case 1: {
                int index = -1;
                for (int i = 0; i < getTables().size(); i++) {
                    if (getTables().get(i).getTableName().equals(table[0])) {
                        index = i;
                    }
                }
                if (-1 == index) {
                    return null;
                }
                // duyet tung dieu kien
                
                
                break;
            }
            case 2:{
                break;
            }
            default:
                return null;
        }
         return result;

    }

    public ArrayList<Row> sqlExcute(String sql,MyError err){
        sql = Command.standardizeString(sql);
        if(sql.indexOf("SELECT") == 0)
            sqlCommand = new Select(sql,this);
        else if(sql.indexOf("DELETE") == 0)
            sqlCommand = new Delete(sql,this);
        else if(sql.indexOf("UPDATE") == 0)
            sqlCommand = new Update(sql,this);
        else if(sql.indexOf("INSERT") == 0)
            sqlCommand = new Insert(sql,this);
        else
            return null;

        return sqlCommand.excute(err);
    }
    /**
     * @return the tables
     */
    public List<Table> getAllTables() {
        return getTables();
    }

    /**
     * 
     * @param tableName
     * @return 
     */
    public Table getTable(String tableName) {
        //tableName = Command.ChuanHoa(tableName);
        for (int i = 0; i < tables.size(); i++) {
            if (getTables().get(i).getTableName().equals(tableName)) {
                return getTables().get(i);
            }
        }
        return null;
    }
    public String GetName()
    {
        return this.name;
    }

    /**
     * @return the tables
     */
    public List<Table> getTables() {
        return tables;
    }

    /**
     * @param tables the tables to set
     */
    public void setTables(List<Table> tables) {
        this.tables = tables;
    }
}
